package com.zyz.I_多线程.Lock锁;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author: vick
 * @Date: 2025/3/24
 * @Description:
 * @version: 1.0
 */
public class Demo1 {
    //1. 创建Lock实例，必须保证多个线程共享同一个Lock实例
    private final ReentrantLock lock = new ReentrantLock();

    private void test() {
        //2. 调用lock()，实现共享代码的锁定
        lock.lock();
        try {
            //保证线程安全的代码
        }finally {
            //3. 调用unlock（），释放共享代码的锁定
            lock.unlock();
        }
    }
}
